package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class DoctorJianKang extends BaseActivity {

	private ImageView pe_head_image;
	private TextView huanzhe;
	private TextView shengao;
	private TextView sex;
	private TextView tizhong;
	private TextView num;
	private TextView bmi;
	private TextView bingshi;

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(
								DoctorJianKang.this, 40))).build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acvitity_ponti);
		intView();

	}

	private String seq;

	private void getdata() {
		seq = getIntent().getStringExtra("seq");
	}

	private Handler Handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String tuu = data.getString(HttpCons.DATA);
			if (state.equals("001")) {

				Gson gson = HttpServer.gson;

				List<Map<String, String>> planList = HttpServer.gson.fromJson(
						tuu, new TypeToken<List<Map<String, String>>>() {
						}.getType());
				if (planList.get(0).get("realname") != null) {
					huanzhe.setText("患者：" + planList.get(0).get("realname"));
					if (planList.get(0).get("sex").toString().equals("1")) {
						sex.setText("性别：" + "男");
					} else {
						sex.setText("性别：" + "女");

					}
				}
				if (planList.get(0).get("headimg") != null) {
					String img = planList.get(0).get("headimg").toString();
					ImageSet(img, pe_head_image);
				} else {
				}
				if (planList.get(0).get("birthday") != null) {
					num.setText("生日："
							+ planList.get(0).get("birthday").toString());
				} else {
				}
				if (planList.get(0).get("height") != null) {
					shengao.setText("身高："
							+ planList.get(0).get("height").toString() + "CM");

				} else {
				}
				if (planList.get(0).get("weight") != null) {
					tizhong.setText("体重："
							+ planList.get(0).get("weight").toString() + "kg");
				}
				if (planList.get(0).get("BMI") != null) {
					bmi.setText("BMI：" + planList.get(0).get("BMI").toString());
				}
				if (planList.get(0).get("content") != null) {
					bingshi.setText("医生评估："
							+ planList.get(0).get("content").toString());
				}

				// ddddd();

				// BitmapUtils bit=new BitmapUtils(CenterInfoActivity.this);
				// bit.display(pe_head_image, img);

				Toast.makeText(DoctorJianKang.this, "操作成功", 0).show();
			} else {
				Toast.makeText(DoctorJianKang.this, "msg", 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(DoctorJianKang.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private void intView() {
		pe_head_image = (ImageView) findViewById(R.id.pe_head_image);
		huanzhe = (TextView) findViewById(R.id.huanzhe);
		shengao = (TextView) findViewById(R.id.shengao);
		sex = (TextView) findViewById(R.id.sex);
		tizhong = (TextView) findViewById(R.id.tizhong);
		num = (TextView) findViewById(R.id.num);
		bmi = (TextView) findViewById(R.id.bmi);
		bingshi = (TextView) findViewById(R.id.bingshi);
		getdata();
		inti();
		initTitlBar();
	}

	public void initTitlBar() {
		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);

		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		tv_title_title.setText(getString(R.string.id));

	}

	private void inti() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("seq", seq);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername(MyConst.getACCOUNT());
		parameter.setPassword("");
		parameter.setCurrentPage("");
		parameter.setPagesize("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.HealthRstimateReportDetail, parameter,
				Handler);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_title_back:
			finish();
			break;

		default:
			break;
		}
	}
}
